<template>
  <div class="login-container">
    <div class="login-content">
      <div class="brand-container">
        <img src="../assets/logo.png" alt="Logo" class="brand-logo" />
        <h1 class="brand-title">Welcome Back</h1>
        <p class="brand-subtitle">Login to continue using our services</p>
      </div>
      
      <el-card class="login-card">
        <el-form :model="form" @submit.prevent="handleLogin" class="login-form">
          <el-alert
            v-if="errorMessage"
            :title="errorMessage"
            type="error"
            show-icon
            :closable="true"
            @close="errorMessage = ''"
            style="margin-bottom: 15px;"
          />
          
          <el-form-item>
            <el-input 
              v-model="form.username" 
              placeholder="Enter username"
              prefix-icon="User"
              :size="'large'"
              class="custom-input"
            />
          </el-form-item>
          
          <el-form-item>
            <el-input 
              v-model="form.password" 
              type="password" 
              placeholder="Enter password"
              prefix-icon="Lock"
              :size="'large'"
              show-password
              class="custom-input"
            />
          </el-form-item>
          
          <div class="form-options">
            <el-checkbox v-model="rememberMe">Remember me</el-checkbox>
            <el-link type="primary" :underline="false" class="forgot-link" @click="showForgotPasswordDialog">Forgot password?</el-link>
          </div>
          
          <el-form-item>
            <el-button 
              type="primary" 
              native-type="submit" 
              :loading="loading"
              class="submit-btn"
              :size="'large'"
            >
              Login
            </el-button>
          </el-form-item>
          
        </el-form>

        <div class="register-link">
          Don't have an account?
          <el-link type="primary" @click="$router.push('/register')" :underline="false" class="signup-link">
            Register now
          </el-link>
        </div>
      </el-card>
    </div>

    <!-- Forgot password dialog -->
    <el-dialog v-model="forgotPasswordDialogVisible" title="Forgot Password" width="400px">
      <p>Please contact the administrator to reset your password.</p>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="forgotPasswordDialogVisible = false">Close</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import axios from '../utils/axios'

const router = useRouter()
const userStore = useUserStore()

const form = ref({
  username: '',
  password: ''
})

const loading = ref(false)
const rememberMe = ref(false)
const errorMessage = ref('')
const forgotPasswordDialogVisible = ref(false)

// Check remembered user data in local storage when page loads
onMounted(() => {
  const savedUsername = localStorage.getItem('rememberedUsername')
  if (savedUsername) {
    form.value.username = savedUsername
    rememberMe.value = true
  }
})

onUnmounted(() => {
  // No need to call stopCamera() as face recognition related code is removed
})

const handleLogin = async () => {
  errorMessage.value = ''
  
  if (!form.value.username.trim()) {
    errorMessage.value = 'Please enter username'
    return
  }
  
  if (!form.value.password) {
    errorMessage.value = 'Please enter password'
    return
  }

  // Handle remember me functionality
  if (rememberMe.value) {
    localStorage.setItem('rememberedUsername', form.value.username.trim())
  } else {
    localStorage.removeItem('rememberedUsername')
  }

  loading.value = true
  try {
    const success = await userStore.login(form.value.username.trim(), form.value.password)
    if (success) {
      ElMessage.success('Login successful')
      router.push('/')
    } else {
      errorMessage.value = userStore.error || 'Login failed, please check username and password'
      console.log('Login failure details:', userStore.error)
    }
  } catch (error: any) {
    console.error('Login exception:', error)
    errorMessage.value = 'An error occurred during login, please try again later'
  } finally {
    loading.value = false
  }
}

// Show forgot password dialog
const showForgotPasswordDialog = () => {
  forgotPasswordDialogVisible.value = true
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  padding: 20px;
  animation: gradientBG 15s ease infinite;
  background-size: 200% 200%;
}

@keyframes gradientBG {
  0% { background-position: 0% 50% }
  50% { background-position: 100% 50% }
  100% { background-position: 0% 50% }
}

.login-content {
  width: 100%;
  max-width: 1000px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2rem;
}

.brand-container {
  text-align: center;
  color: white;
  margin-bottom: 1rem;
  animation: fadeInDown 0.8s ease;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.brand-logo {
  width: 100px;
  height: 100px;
  margin-bottom: 1.5rem;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.1));
  transition: transform 0.3s ease;
}

.brand-logo:hover {
  transform: scale(1.05);
}

.brand-title {
  font-size: 2.5rem;
  font-weight: 600;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  letter-spacing: -0.5px;
}

.brand-subtitle {
  font-size: 1.1rem;
  opacity: 0.95;
  margin: 0.75rem 0 0;
  font-weight: 300;
}

.login-card {
  width: 100%;
  max-width: 400px;
  border-radius: 24px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 40px;
  animation: fadeIn 0.8s ease;
  border: none;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-form {
  margin-top: 0;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0.5rem 0 1.5rem;
}

.submit-btn {
  width: 100%;
  height: 48px;
  font-size: 1.1rem;
  border-radius: 12px;
  margin: 0;
  background: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
  border: none;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(79, 172, 254, 0.3);
}

.submit-btn:active {
  transform: translateY(0);
}

.alternative-login {
  margin-top: 1.5rem;
  text-align: center;
}

.divider {
  position: relative;
  margin: 15px 0;
  text-align: center;
  color: #999;
  font-size: 0.9rem;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 45%;
  height: 1px;
  background-color: #e0e0e0;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

/* Face recognition styles removed as they are no longer needed */

.register-link {
  text-align: center;
  margin-top: 2rem;
  color: #666;
  font-size: 0.95rem;
}

.signup-link {
  font-weight: 500;
  margin-left: 0.5rem;
  transition: color 0.3s ease;
}

.signup-link:hover {
  color: #4facfe;
}

.forgot-link {
  font-size: 0.95rem;
  cursor: pointer;
}

.forgot-link:hover {
  text-decoration: underline;
}

:deep(.el-input__wrapper) {
  border-radius: 12px;
  height: 48px;
  background: #f8fafc;
  box-shadow: none;
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  background: #fff;
  border-color: #e2e8f0;
}

:deep(.el-input__wrapper.is-focus) {
  background: #fff;
  border-color: #4facfe;
  box-shadow: 0 0 0 4px rgba(79, 172, 254, 0.1);
}

:deep(.el-input__inner) {
  font-size: 1rem;
  color: #334155;
}

:deep(.el-input__inner::placeholder) {
  color: #94a3b8;
}

:deep(.el-checkbox__label) {
  font-size: 0.9rem;
  color: #64748b;
}

@media (max-width: 768px) {
  .login-card {
    margin: 0 20px;
    padding: 30px;
  }
  
  .brand-title {
    font-size: 2rem;
  }
  
  .brand-subtitle {
    font-size: 1rem;
  }

  .submit-btn {
    height: 44px;
  }

  :deep(.el-input__wrapper) {
    height: 44px;
  }
}
</style>